﻿/*----------------------------------------------------------------
// Copyright (C) 2012 Q.FunnyFace
// 版权所有。 
//
// 文件名：HomeController.cs
// 文件功能描述：主页控制器
//
// 
// 创建标识：2012-10-26 zhangqiang
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using Common;
using DomainModel;
using EmitMapper;
using IManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebPlatform.FiltersAndSession;
using WebPlatform.Models;

namespace WebPlatform.Controllers
{

    /// <summary>
    /// 主页控制器
    /// </summary>
    [CustomAuthorize]
    public class HomeController : Controller
    {

        /// <summary>
        /// 登录会话操作
        /// </summary>
        public Sessions Sessions { get; set; }


        /// <summary>
        /// 用户信息业务操作
        /// </summary>
        public IManagerUser IMUser { get; set; }


        /// <summary>
        /// 模块信息业务操作
        /// </summary>
        public IManagerModule IMModule { get; set; }


        /// <summary>
        /// 主控页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var user = Sessions.User;
            if (null != user)
            {
                ViewBag.UserName = user.UserName;
            }

            return View();
        }



        /// <summary>
        /// 欢迎页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Welcome()
        {
            return View();
        }


        /// <summary>
        /// 注销
        /// </summary>
        /// <returns>登录页面</returns>
        public ActionResult LoginOut()
        {
            string entry = Sessions.User.Entry;
            Sessions.ClearUser();
            if (entry == "Front") {
                return RedirectToRoute("CPP_default", new { controller = "Default", action = "Index" });
            }
            else
            {
                return RedirectToAction("Index", "Login");
            }
            
        }



        /// <summary>
        /// 修改密码视图
        /// </summary>
        /// <returns></returns>
        public ActionResult ChangePassword()
        {
            return View();
        }


        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="passwordVM"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ChangePassword(PasswordVM passwordVM)
        {
            var flag = false;
            if (ModelState.IsValid)
            {
                flag = IMUser.ChangePassword(Sessions.User.UserID, passwordVM.NewPassword);
            }
            return Json(flag);
        }



        /// <summary>
        /// 验证密码
        /// </summary>
        /// <param name="UserName">旧密码</param>
        /// <returns>false 存在:提示存在 true不存在通过验证  </returns>
        public JsonResult IsTruePassword(string OldPassword)
        {
            if (string.IsNullOrEmpty(OldPassword))
            {
                return Json(false);
            }

            UserInfo ui = IMUser.GetUser(Sessions.User.UserID);

            if (ui == null || ui.UserID == 0 || ui.LoginPassword != Encrypt.MD5(OldPassword))
            {
                return Json(false);
            }
            return Json(true);
        }


        /// <summary>
        /// 验证密码
        /// </summary>
        /// <param name="UserName">新密码</param>
        /// <param name="UserName">确认密码</param>
        /// <returns>false 存在:提示存在 true不存在通过验证  </returns>
        public JsonResult IsSamePassword(string NewPassword, string ConfirmPassword)
        {
            return Json(NewPassword == ConfirmPassword);
        }



        /// <summary>
        /// 加载模块
        /// </summary>
        /// <returns></returns>
        public JsonResult Module()
        {
            ObjectsMapper<ModuleInfo, ModuleVM> mapper = ObjectMapperManager
                                                        .DefaultInstance
                                                        .GetMapper<ModuleInfo, ModuleVM>();
            var userType = Sessions.User.UserType.ToString();
            var parents = IMModule.GetParentsModules(userType);
            IList<ModuleVM> modules = mapper.MapEnum(parents).ToList();
            IList<ModuleVM> treeData = new List<ModuleVM>();

            foreach (var item in modules)
            {
                var subModules = IMModule.GetSubsModules(userType, item.ModuleID);
                var subs = mapper.MapEnum(subModules);

                item.id = item.ModuleID;

                if (subs == null || subs.Count() == 0)
                {
                    item.isLeaf = true;
                }
                else
                {
                    item.isLeaf = false;
                    item.isexpand = true;
                    foreach (var itemSub in subs)
                    {
                        itemSub.isLeaf = true;
                        itemSub.id = itemSub.ModuleID;
                        item.children.Add(itemSub);
                    }
                }
                treeData.Add(item);
            }

            return Json(modules, JsonRequestBehavior.AllowGet);
        }


    }
}
